﻿using Microsoft.AspNetCore.Diagnostics;
using Newtonsoft.Json;
using System.Net;

namespace DbJobsApi
{
    public class Startup
    {
        public static void Run(int port)
        {
            Host.CreateDefaultBuilder()
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseUrls($"http://localhost:{port}");
                        webBuilder.UseStartup<Startup>();
                    })
                    .Build() 
                    .Run();
        }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(Configuration);
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddLogging(builder =>
            {
                builder.ClearProviders();
                builder.AddSimpleConsole(b => b.SingleLine = true);
            });
            services.AddControllers()
                    .AddNewtonsoftJson(options =>
                    {
                        options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                        options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
                    });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();
            app.UseStaticFiles();
            app.UseExceptionHandler(builder =>
            {
                builder.Run(async context =>
                {
                    context.Response.StatusCode = 500;
                    context.Response.ContentType = "application/json";

                    var exception = context.Features.Get<IExceptionHandlerFeature>();
                    if (exception != null)
                    {
                        var errObj = JsonConvert.SerializeObject(
                            new
                            {
                                status = context.Response.StatusCode,
                                statusText = exception.Error.Message
                            });
                        await context.Response.WriteAsync(errObj).ConfigureAwait(false);
                    }
                });
            });
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapGet("/check", async context =>
                {
                    await context.Response.WriteAsync($"Health Ckeck {System.DateTime.Now}");
                });
            });
        }
    }
}
